﻿using System;
using System.Linq;
using System.Web.Mvc;
using Inscriptionem.Areas.Inscriptionem.Models.Requests;
using Inscriptionem.Areas.Inscriptionem.Models.Views;
using Inscriptionem.Authorization;
using Inscriptionem.Controllers;
using Inscriptionem.Services;

namespace Inscriptionem.Areas.Inscriptionem.Controllers
{
    [InscriptionemAuthorize(MemberRole.Admin)]
    public class ContactController : Controller
    {
        private readonly ORM.DB DB;
        public ContactController()
        {
            DB = new ORM.DB();
        }

        public ActionResult Index()
        {
            return View("Index", new ContactCollectionViewModel());
        }

        [HttpPost]
        public ActionResult Reply(ContactReplyRequestModel model)
        {
            if (model.ID <= 0)
            {
                // Invalid id
                return RedirectToAction("Index");
            }

            var request = DB.ContactRequests.FirstOrDefault(cr => cr.ID == model.ID);
            if (request == null)
            {
                // Contact request not found
                return RedirectToAction("Index");
            }

            string errorMessage = string.Empty;

            // Reply
            if (!string.IsNullOrWhiteSpace(model.Message))
            {
                try
                {
                    var response = new ORM.ContactResponse()
                                       {
                                           Message = model.Message,
                                           ContactRequest = request,
                                           Member = DB.Members.First(m => m.ID == Member.Current.ID),
                                           CreatedDate = DateTime.Now,
                                           ModifiedDate = DateTime.Now
                                       };

                    DB.AddToContactResponses(response);
                    DB.SaveChanges();

                    MailService.SendMail(request.Mail, Configuration.Configuration.Instance.General.Title, model.Message);
                }
                catch (Exception ex)
                {
                    errorMessage = ex.ToString();
                }
            }

            return View("Reply", new ContactViewModel()
            {
                ID = request.ID,
                Message = request.Message,
                Mail = request.Mail,
                Member = request.Member,
                Error = errorMessage,
                Responses = request.ContactResponses
            });
        }

        [HttpPost]
        public ActionResult Delete(int ID)
        {
            if (ID <= 0)
            {
                return RedirectToAction("Index");
            }

            var request = DB.ContactRequests.FirstOrDefault(cr => cr.ID == ID);
            if (request == null)
            {
                return RedirectToAction("Index");
            }

            try
            {
                request.IsDeleted = true;
                request.ModifiedDate = DateTime.Now;
                DB.SaveChanges();
            }
            catch (Exception)
            {

            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Search(string Text, int Page)
        {
            if (Page < 0)
            {
                Page = 0;
            }

            var result = new ContactSearchCollectionViewModel { Page = Page, Text = Text };

            return View("Search", result);
        }

        public ActionResult Page(int ID)
        {
            if (ID <= 0)
            {
                return RedirectToAction("Index");
            }

            return View("Index", new ContactCollectionViewModel() { Page = ID });
        }

        #region Pagination
        public ActionResult Pagination(global::Inscriptionem.Models.Menu.Requests.PaginationRequestModel model)
        {
            return new MenuController().Pagination(model);
        }
        #endregion

    }
}
